<?php
/**
 * Created by PhpStorm.
 * User: stupboy
 * Date: 2018/5/29
 * Time: 17:31
 * Mail:87658989@qq.com
 */

namespace app\adminapi\controller;
use think\Db;
use app\adminapi\model;
use app\adminapi\validate\CarValidate;
use app\adminapi\validate\Report;
use app\base\service\Csv;
use app\open\validate\StatValidate;
/**
 * @name【车辆管理】
 * @baseurl【adminapi/Car】
 * @file【Car.php】
 */
class Car extends Base
{

    /**
     * @name【车辆统计】
     * @desc【车辆统计】
     * @method【post】
     * @table【p_car,p_car_type,p_maintenance_record,p_mot_record,p_refuel_record,p_car_insure】
     * @group【life】
     * @param【
     * car_id $ int $ 0 $ 车辆ID  #
     * company_id $ int $ 0 $ 公司ID  #
     * entrust_company_id $ int $ 0 $ 委托公司ID  #
     * prop $ int $ 0 $ 车辆性质  #
     * filter $ int $ 0 $ 筛选?  #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * car_number $ int $  $ 汽车总数#
     * gongwuche_number $ int $  $ 公务车总数#
     * search_car_number $ int $  $ 搜索结果总数#
     * maintenance_record_number $ int $  $ 维保总额#
     * mot_record_number $ int $  $ 年检总额#
     * refuel_record_number $ int $  $ 加油总额#
     * car_insure_number $ int $  $ 保险总额#
     * oil_litre $ array $  $ 每月油量#
     * oil_price $ array $  $ 每月油费#
     * 】
     * @author 【yulin<570214321@qq.com>】
     */
    public function carStat(){

        $report = new StatValidate();
        $param = $report->scene('car_stat')->getCurrentData();
        $report->goCheck($param);
        $where = [];
        if($param['filter'] == 1){
            $where['is_beidou'] = ['=', "1"];
        } elseif($param['filter'] == 2) {
            $where['is_beidou'] = ['=', "1"];
            $where['entrust_company_id'] = ['=', "0"];
        }
        $param['car_id'] && $where['car_id'] = ['=', "%{$param['car_id']}%"];
        $param['company_id'] && $where['company_id'] = ['=', $param['company_id']];
        $param['entrust_company_id'] && $where['entrust_company_id'] = ['=', "%{$param['entrust_company_id']}%"];
        $param['prop'] && $where['prop'] = ['=', $param['prop']];
        //汽车总数
        $return_1 = (new model\Car())
            ->select();
        $data['car_number'] = count($return_1);

        //公务车总数
        $return_2 = (new model\CarType())
            ->where('type',2)
            ->field('car_type_id')
            ->select();
        $data['gongwuche_number'] = 0;
        foreach ($return_2 as $key2 => $val2) {
            $return_2_1 = (new model\Car())
                ->where('car_type_id',$return_2[$key2]['car_type_id'])
                ->select();
            $data['gongwuche_number'] += count($return_2_1);
        }

        //搜索结果总数
        $return_3 = (new model\Car())
            ->where($where)
            ->select();
        $data['search_car_number'] = count($return_3);


        $data['maintenance_record_number'] = 0;//维保总额
        $data['mot_record_number'] = 0;//年检总额
        $data['refuel_record_number'] = 0;//加油总额
        $data['car_insure_number'] = 0;//保险总额
        $data['oil_litre'] = array();//每月油量
        $data['oil_price'] = array();//每月油费


        //维保总额
        $data['maintenance_record_number'] = (new model\Car())
            ->alias('a')
            ->join('maintenance_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->sum('b.price');

        //年检总额
        $data['mot_record_number'] = (new model\Car())
            ->alias('a')
            ->join('mot_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->sum('b.price');

        //加油总额
        $data['refuel_record_number'] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->sum('b.price');

        //保险总额
        $data['car_insure_number'] = (new model\Car())
            ->alias('a')
            ->join('car_insure b','b.car_id = a.car_id')
            ->field('b.policy_charge')
            ->where($where)
            ->sum('b.policy_charge');

        //每月加油金额
        $data['oil_litre'][1] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-01-%')
            ->sum('b.price');
        $data['oil_litre'][2] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-02-%')
            ->sum('b.price');
        $data['oil_litre'][3] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-03-%')
            ->sum('b.price');
        $data['oil_litre'][4] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-04-%')
            ->sum('b.price');
        $data['oil_litre'][5] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-05-%')
            ->sum('b.price');
        $data['oil_litre'][6] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-06-%')
            ->sum('b.price');
        $data['oil_litre'][7] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-07-%')
            ->sum('b.price');
        $data['oil_litre'][8] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-08-%')
            ->sum('b.price');
        $data['oil_litre'][9] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-09-%')
            ->sum('b.price');
        $data['oil_litre'][10] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-10-%')
            ->sum('b.price');
        $data['oil_litre'][11] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-11-%')
            ->sum('b.price');
        $data['oil_litre'][12] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-12-%')
            ->sum('b.price');

        //每月加油量
        $data['oil_price'][1] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-01-%')
            ->sum('b.litre');
        $data['oil_price'][2] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-02-%')
            ->sum('b.litre');
        $data['oil_price'][3] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-03-%')
            ->sum('b.litre');
        $data['oil_price'][4] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-04-%')
            ->sum('b.litre');
        $data['oil_price'][5] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-05-%')
            ->sum('b.litre');
        $data['oil_price'][6] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-06-%')
            ->sum('b.litre');
        $data['oil_price'][7] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-07-%')
            ->sum('b.litre');
        $data['oil_price'][8] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-08-%')
            ->sum('b.litre');
        $data['oil_price'][9] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-09-%')
            ->sum('b.litre');
        $data['oil_price'][10] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-10-%')
            ->sum('b.litre');
        $data['oil_price'][11] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-11-%')
            ->sum('b.litre');
        $data['oil_price'][12] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-12-%')
            ->sum('b.litre');
        for($i=1;$i<=12;$i++){
            $data['oil_litre'][$i] = round($data['oil_litre'][$i] ,2);;
            $data['oil_price'][$i] = round($data['oil_litre'][$i] ,2);;
        }

        return $this->retSucc($data);
    }

    /**
     * @name【车辆列表】
     * @desc【车辆列表】
     * @uri【carList】
     * @method【post】
     * @param【
     * token $ string $ abc $ 验证字符 $ 1 #
     * uid $ int $ 1 $ 用户id $ 1 #
     * fields $ string $ driver_id,name $ 字段名称 #
     * where_json $ json $ {"order_id":["in","6,7,8"],"name":["like","%王%"]} $ 查询条件 #
     * page $ int $ 1$ 页数 #
     * pagenum $ int $ 10 $记录数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息 #
     * data $ json $ $ 订单列表 #
     * description $ json $ $ 接口说明
     * 】
     */
    public function carList(){
        $report    = new Report();
        $param     = $report->scene('report')->getCurrentData();
        $report->goCheck($param);
        $where     = json_decode($param['where_json'],true);
        list($code,$data) = (new model\Car())->carLlist($param['fields'],$where,$param['page'],$param['pagenum']);
        if($code==1){
            return $this->retSucc($data);
        }else{
            return $this->retError($data);
        }
    }

    /**
     * @name【车辆列表下载】
     * @desc【车辆列表下载】
     * @uri【downLoadCarList】
     * @method【post】
     * @param【
     * token $ string $ abc $ 验证字符 $ 1 #
     * uid $ int $ 1 $ 用户id $ 1 #
     * fields $ string $ order_id,passenger_name $ 字段名称 #
     * where_json $ json $ {"order_id":["in","6,7,8"],"name":["like","%王%"]} $ 查询条件 #
     * counts $ int $ 1$ 下载数据记录数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 校验码#
     * msg $ string $  $ 错误信息 #
     * data $ json $ $ 订单列表 #
     * description $ json $ $ 接口说明
     * 】
     */
    public function downLoadCarList(){
        set_time_limit(300);
        $csv       = new Csv();
        $report    = new Report();
        $param     = $report->scene('download')->getCurrentData();
        $report->goCheck($param);
        $where     = json_decode($param['where_json'],true);
        $totalPage = $param['counts'] % 2000 == 0 ? $param['counts']/2000 : floor($param['counts']/2000) + 1;
        $f = [];
        for($i=1;$i<=$totalPage;$i++){
            list($code,$data) = (new model\Car())->carLlist($param['fields'],$where,$i,2000);
            if($code==0) continue;
            if(empty($data['data'])) break;
            $csv->open($data['fields']);
            //以下by玉林
            foreach ($data['data'] as $key => $val) {

                if($data['data'][$key]['gearbox_type'] == '1'){
                    $data['data'][$key]['gearbox_type'] = '手动';
                }
                elseif ($data['data'][$key]['gearbox_type'] == '2'){
                    $data['data'][$key]['gearbox_type'] = '自动';
                }
                elseif ($data['data'][$key]['gearbox_type'] == '3'){
                    $data['data'][$key]['gearbox_type'] = '手自一体';
                }

                if($data['data'][$key]['source']==1){
                    $data['data'][$key]['source']='单位直属公务车';
                }
                else if($data['data'][$key]['source']==2){
                    $data['data'][$key]['source']='管理公司直属公务车';
                }
                else if($data['data'][$key]['source']==3){
                    $data['data'][$key]['source']='单位自有车辆';
                }
                else{
                    $data['data'][$key]['source']='来源未知';
                }

                if($data['data'][$key]['energy_type']==1){
                    $data['data'][$key]['energy_type']='电动';
                }
                else if($data['data'][$key]['energy_type']==2){
                    $data['data'][$key]['energy_type']='汽油';
                }
                else if($data['data'][$key]['energy_type']==3){
                    $data['data'][$key]['energy_type']='柴油';
                }
                else{
                    $data['data'][$key]['energy_type']='类型未知';
                }

                if($data['data'][$key]['structure']==1){
                    $data['data'][$key]['structure']='两厢车';
                }
                else if($data['data'][$key]['structure']==2){
                    $data['data'][$key]['structure']='三厢车';
                }
                else if($data['data'][$key]['structure']==3){
                    $data['data'][$key]['structure']='SUV';
                }
                else{
                    $data['data'][$key]['structure']='车身结构未知';
                }

                if($data['data'][$key]['structure']==1){
                    $data['data'][$key]['structure']='两厢车';
                }
                else if($data['data'][$key]['structure']==2){
                    $data['data'][$key]['structure']='三厢车';
                }
                else if($data['data'][$key]['structure']==3){
                    $data['data'][$key]['structure']='SUV';
                }
                else{
                    $data['data'][$key]['structure']='车身结构未知';
                }
            }
            //以上by玉林
            $csv->writeAll($data['data']);
            $f[] = $csv->myClose();
        }
        $csv->mergeCsvToDownload($f);
    }


    /**
     * @name【新增车辆】
     * @desc【新增车辆】
     * @uri【add】
     * @method【post】
     * @param【
     * token $ string $ abc $ 验证字符 $ 1 #
     * uid $ int $ 1 $ 用户id $ 1 #
     * company_id $ int $ 1 $ 单位id $ 1 #
     * entrust_company_id $ int $ 0 $ 委托公司为0则不委托 $ 1 #
     * car_type_id $ int $ 1 $ 车型id $ 1 #
     * car_brand_id $ int $ 1 $ 车辆品牌id $ 1 #
     * car_model_id $ int $ 1 $ 车型 #
     * car_series_id $ int $ 1 $ 系列 #
     * number $ string $ 默认值 $ 车牌 $ 1 #
     * source $ int $ 1 $ 车辆来源 #
     * structure $ int $ 1 $ 车型结构1两厢 2三厢 3 suv 4其他 $ 1 #
     * colour $ int $ 1 $ 车身颜色 #
     * gearbox_type $ int $ 1 $ 变速类型 1手动 2自动 3 手自动一体 $ 1 #
     * displacement $ int $ 1 $ 排量 $ 1 #
     * car_age $ int $ 1 $ 车龄 #
     * energy_type $ int $ 1 $ 能源类型 1 电 2汽油 3柴油 4 混合 #
     * engine_no $ string $ 2377777 $ 发动机号 $ 1 #
     * chassis_no $ string $ 2377777 $ 底盘号 $ 1 #
     * vin $ string $ 2377777 $ 车辆识别号 $ 1 #
     * carry $ int $ 1 $ 承载人数 $ 1 #
     * buy_date $ date $ 1997-1-1 $ 购买日期 #
     * buy_money $ int $ 1 $ 购买金额 #
     * mileage $ int $ 1 $ 行驶里程 #
     * driving_license_issued $ date $ 1997-1-1 $ 行驶证发证日期 $ 1 #
     * driving_license_effective $ date $ 1997-1-1 $ 行驶证有效日期 $ 1 #
     * prop $ int $ 1 $ 车辆性质 #
     * state $ int $ 1 $ 车辆状态 1启用 9禁用 $ 1 #
     * driving_license_img $ string $  $ 行驶证正本图片 #
     * driving_license_img2 $ string $  $ 行驶证副本图片 #
     * front_img $ string $  $ 车正面图片 #
     * back_img $ string $  $ 车辆背面图片 #
     * side_img $ string $  $ 车辆45度侧面图片 #
     * engine_img $ string $  $ 车辆发动机号图片 #
     * business_insurance_company $ string $  $ 商业险保险公司 #
     * business_policy_no $ string $  $ 商业险保单号 #
     * business_policy_effective $ string $  $ 商业险保单有效期 #
     * phone $ string $ 13987897655 $ 商业险报销公司电话 #
     * rent_company $ string $  $ 租赁公司 #
     * business_policy_img $ string $  $ 商业险保单图片 #
     * compulsory_policy_no $ string $  $ 交强险保单号 #
     * compulsory_policy_effective $ date $ 1997-1-1 $ 交强险保险有效期#
     * compulsory_policy_img $ string $  $ 交强险#
     * register_time $ date $ 1997-1-1 $ 注册时间  #
     * car_audit_id $ int $ 1 $ 新购车辆单ID  #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * maintenance_id $ int $ 1 $ 新增成功的车辆ID#
     * 】
     * @return mixed|\think\response\Json|void
     * @throws \app\base\exception\RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){
        // 校验请求参数
        $validate = new CarValidate();
        // 获取所有请求参数
        $params = $validate->scene('add')->goCheck()->getCurrentData();

        $car = (new model\Car()) ->createOrUpdate($params);

        //以下by玉林
        if($params['car_audit_id']){
            Db::name('car_audit')->where('car_audit_id', $params['car_audit_id'])->update(['state' => '4']);
        }
        //以上by玉林

        return $this->retSucc(['car_id' => $car->car_id]);
    }

    /**
     * @name【编辑车辆】
     * @desc【编辑车辆】
     * @uri【add】
     * @method【post】
     * @param【
     * token $ string $ abc $ 验证字符 $ 1 #
     * uid $ int $ 1 $ 用户id $ 1 #
     * car_id $ int $ 1 $ 车辆id $ 1 #
     * company_id $ int $ 1 $ 单位id $ 1 #
     * entrust_company_id $ int $ 0 $ 委托公司为0则不委托 $ 1 #
     * car_type_id $ int $ 1 $ 车型id $ 1 #
     * car_brand_id $ int $ 1 $ 车辆品牌id $ 1 #
     * car_model_id $ int $ 1 $ 车型 #
     * car_series_id $ int $ 1 $ 系列 #
     * number $ string $ 默认值 $ 车牌 $ 1 #
     * source $ int $ 1 $ 车辆来源 #
     * structure $ int $ 1 $ 车型结构1两厢 2三厢 3 suv 4其他 $ 1 #
     * colour $ int $ 1 $ 车身颜色 #
     * gearbox_type $ int $ 1 $ 变速类型 1手动 2自动 3 手自动一体 $ 1 #
     * displacement $ int $ 1 $ 排量 $ 1 #
     * car_age $ int $ 1 $ 车龄 #
     * energy_type $ int $ 1 $ 能源类型 1 电 2汽油 3柴油 4 混合 #
     * engine_no $ string $ 2377777 $ 发动机号 $ 1 #
     * chassis_no $ string $ 2377777 $ 底盘号 $ 1 #
     * vin $ string $ 2377777 $ 车辆识别号 $ 1 #
     * carry $ int $ 1 $ 承载人数 $ 1 #
     * buy_date $ date $ 1997-1-1 $ 购买日期 #
     * buy_money $ int $ 1 $ 购买金额 #
     * mileage $ int $ 1 $ 行驶里程 #
     * driving_license_issued $ date $ 1997-1-1 $ 行驶证发证日期 $ 1 #
     * driving_license_effective $ date $ 1997-1-1 $ 行驶证有效日期 $ 1 #
     * prop $ int $ 1 $ 车辆性质 #
     * state $ int $ 1 $ 车辆状态 1启用 9禁用 $ 1 #
     * driving_license_img $ string $  $ 行驶证正本图片 #
     * driving_license_img2 $ string $  $ 行驶证副本图片 #
     * front_img $ string $  $ 车正面图片 #
     * back_img $ string $  $ 车辆背面图片 #
     * side_img $ string $  $ 车辆45度侧面图片 #
     * engine_img $ string $  $ 车辆发动机号图片 #
     * business_insurance_company $ string $  $ 商业险保险公司 #
     * business_policy_no $ string $  $ 商业险保单号 #
     * business_policy_effective $ string $  $ 商业险保单有效期 #
     * phone $ string $ 13987897655 $ 商业险报销公司电话 #
     * rent_company $ string $  $ 租赁公司 #
     * business_policy_img $ string $  $ 商业险保单图片 #
     * compulsory_policy_no $ string $  $ 交强险保单号 #
     * compulsory_policy_effective $ date $ 1997-1-1 $ 交强险保险有效期#
     * compulsory_policy_img $ string $  $ 交强险#
     * register_time $ date $ 1997-1-1 $ 注册时间  #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * updated $ int $  1 $ 更新记录条数#
     * 】
     * @throws \app\base\exception\RecordNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){
        // 校验请求参数
        $validate = new CarValidate();
        // 获取所有请求参数
        $params = $validate->scene('edit')->goCheck()->getCurrentData();

        $result = (new model\Car()) ->createOrUpdate($params);

        return $this->retSucc(['updated' => $result]);
    }

    /**
     * @name【删除车辆】
     * @desc【删除车辆】
     * @uri【delete】
     * @method【post】
     * @param【
     * car_ids $ int $ 1 $ 车辆ID，多个请用','号连接 $ 1 #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * deleted $ int $  1 $ 删除的记录条数#
     * 】
     */
    public function delete(){
        // 校验请求参数
        $validate = new CarValidate();
        // 获取所有请求参数
        $params = $validate->scene('delete')->goCheck()->getCurrentData();

        $where = ['car_id' => ['IN', explode(',', $params['car_ids'])]];
        $buildQuery = function($query) use ($where) {
            model\Common::authCompanyQuery($query, $where, 'a.company_id');
        };

        $result = model\Car::destroy($buildQuery);

        return $this->retSucc(['deleted' => $result]);
    }

    /**
     * @name【获取品牌】
     * @desc【获取品牌】
     * @method【post】
     * @param【
     * id $ int $ 1 $ 品牌ID $ 1 #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * deleted $ int $  1 $ 删除的记录条数#
     * 】
     */
    public function brands() {

        $id = input('param.id/d');
        $car_brands = Db::name('car_brand')->where('parent_id',$id)->fetchSql(0)->column('name','car_brand_id');

        return $this->retSucc(['data' => $car_brands]);

    }

    /**
     * @name【获取车型】
     * @desc【获取车型】
     * @method【post】
     * @param【
     * id $ int $ 1 $ 车型ID $ 1 #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * deleted $ int $  1 $ 删除的记录条数#
     * 】
     */
    public function cartypes() {
        $car_types = Db::name('car_type')->column('car_type_name','car_type_id');
        return $this->retAll($car_types);
    }

    /**
     * @name【获取车型】
     * @desc【获取车型】
     * @method【post】
     * @param【
     * car_type_id $ int $ 1 $ 车型ID  #
     * type $ int $ 2 $ 用车类型:2公务车 3公包车 4约租车 5约包车 #
     * car_type $ int $ 2 $车型: 2优选 3舒适 9商务 #
     * seats $ int $ 1 $座位数
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * data $ array $$ 车型数组 #
     * @ car_type_name $ string $$ 车型名称#
     * @ car_type_id $ int $$ 车型id #
     * @ seats $ int $$ 座位数 #
     * @ type $ int $$ 用车类型 #
     * @ car_type $ int $$ 车型 #
     * @ chartered_km $ int $$ 包车里程 #
     * @ chartered_hour $ int $$ 包车小时 #
     * @ chartered_charge $ int $$ 包车费用 #
     * deleted $ int $  1 $ 删除的记录条数#
     * 】
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function carTypes1() {
        $string = 'type#用车类型@number/car_type#车型@number/';
        $string.= 'seats#座位数@number/car_type_id#车型id@number';
        $where  = $this->getBatchParam($string);
        $newWhere = [];
        foreach ($where as $k=>$v){
            if(!empty($v)){
                $newWhere[$k] = $v;
            }
        }
        $fields = 'car_type_name,car_type_id,case when car_type = 9 then 7 when car_type between 2 and 3 then 5 else seats end seats,type,car_type,chartered_km,chartered_hour,p_chartered_charge chartered_charge';
        $data['data'] = Db::name('car_type')
            ->whereNull('delete_time')
            ->where($newWhere)
            ->field($fields)
            ->select();
        return $this->retSucc($data);
    }

}